package com.cssl.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cssl.domain.DtsGoods;
import com.cssl.domain.DtsTopic;
import com.cssl.mapper.DtsTopicMapper;
import com.cssl.service.DtsTopicService;
import com.cssl.util.InfoCode;
import com.cssl.util.ObjectMapperUtil;
import com.cssl.util.Result;
import com.cssl.util.StorageVariable;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class DtsTopicServiceImpl implements DtsTopicService {
    @Resource
    private DtsTopicMapper topicMapper;
    @Resource
    private StringRedisTemplate template;
    @Resource
    private ObjectMapperUtil objectMapperUtil;

    @Override
    public List<DtsTopic> getTopicList() {
        //从redis中获取(获得的全部)
        String s = template.opsForValue().get(StorageVariable.TOPIC);
        if (StrUtil.isEmpty(s)) {
            //空
            List<DtsTopic> topicList = topicMapper.getTopicList();
            template.opsForValue().set(StorageVariable.TOPIC, objectMapperUtil.toJsonString(topicList), 7, TimeUnit.DAYS);
            return topicList;
        }
        //不为空
        List<DtsTopic> topicList = objectMapperUtil.fromJsonString(s, new TypeReference<List<DtsTopic>>() {
        });
        return topicList;
    }

    @Override
    public Result getTopicById(Map map) {
        Integer id = Integer.parseInt(map.get("id").toString());
        //先从redis获取
        String topic = template.opsForValue().get(StorageVariable.TOPIC + ":" + id);
        String goodsList = template.opsForValue().get(StorageVariable.TOPICGOODS + ":" + id);
        Map maps = new HashMap();
        if (StrUtil.isEmpty(topic) || StrUtil.isEmpty(goodsList)) {
            //为空
            DtsTopic topicList = topicMapper.getTopicById(id);
            template.opsForValue().set(StorageVariable.TOPIC + ":" + id, objectMapperUtil.toJsonString(topicList));
            String[] split = topicList.getGoods().substring(1, topicList.getGoods().length() - 1).split(",");
            List<String> strings = Arrays.asList(split);
            List<DtsGoods> goods = topicMapper.getGoodsByGoodsSn(strings);//查询topic对应的商品
            template.opsForValue().set(StorageVariable.TOPICGOODS + ":" + id, objectMapperUtil.toJsonString(goods));
            maps.put("topic", topicList);
            maps.put("topicGoods", goods);
            return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, maps, System.currentTimeMillis());
        }

        DtsTopic dtsTopic = objectMapperUtil.fromJsonString(topic, new TypeReference<DtsTopic>() {
        });
        List<DtsGoods> goods = objectMapperUtil.fromJsonString(goodsList, new TypeReference<List<DtsGoods>>() {
        });
        maps.put("topic", dtsTopic);
        maps.put("topicGoods", goods);
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, maps, System.currentTimeMillis());
    }

    @Override
    public Result getTopicNotById(DtsTopic topic) {
        return Result.result(InfoCode.OK, InfoCode.MESSAGE_OK, topicMapper.getTopicNotById(topic.getId()), System.currentTimeMillis());
    }
}
